package com.myacg.platform.ui.event.move;

import java.awt.Component;

import com.myacg.core.entity.Point;
import com.myacg.core.move.MoveAdapter;
import com.myacg.core.move.MoveEngine;
import com.myacg.core.move.MoveListener;
import com.myacg.core.move.MovePlugin;
import com.myacg.core.utils.MoveUtils;

/**
 * 为控件添加移动效果的事件
 * @author Administrator
 *
 */
public abstract class BaseLineMoveToEvent implements ComponentMoveToEvent{
	protected double length;
	protected Point  endPoint;
	protected double initF,initV0;
    protected Component comp;
    protected MoveListener movelistener;
    protected MoveEngine moveEngine;
    protected MovePlugin movePlugin;
    
    
	public BaseLineMoveToEvent(Component comp){
		 this.comp=comp;
		 initMoveLinstener();//初始化事件监听器
	     java.awt.Point point = comp.getLocation();
	     this.moveEngine= new MoveEngine(new Point(comp.getLocation().x, comp.getLocation().y), movelistener);
		 //获取移动插件
		 this.movePlugin=moveEngine.getMovePlugin();
		 
		 //暂停
		 this.moveEngine.pause();
	}
	
	public BaseLineMoveToEvent(Component comp,MoveEngine moveEngine){
		 this.comp=comp;
		 initMoveLinstener();//初始化事件监听器
	     java.awt.Point point = comp.getLocation();
		 this.moveEngine= moveEngine;
		 //获取移动插件
		 this.movePlugin=moveEngine.getMovePlugin();
		 
		 //暂停
		 this.moveEngine.pause();
	}
	
	
	//移动到某个位置
    public void moveTo(Point endPoint,double F , double V0){
    	this.endPoint=endPoint;
    	this.initF=F;
    	this.initV0=V0;
    	
    	//计算目标点和方向
    	Point selfPoint = new Point(comp.getLocation().x, comp.getLocation().y);
    	//获取与目标点之间的差距
    	length=MoveUtils.getRangle(selfPoint, endPoint);
    	//获取角度
    	if(length!=0){
    		//System.out.println("aaa");
    		//重设方向
    		double direction = MoveUtils.getAngle(selfPoint, endPoint);
    		movePlugin.setDirection(direction);
    		//重设引擎内核起始点
    		Point p =movePlugin.getPoint();
    		p.x=selfPoint.x;
    		p.y=selfPoint.y;
    		//重置运行状态
        	movePlugin.resetState();
        	//给予某个速度点
        	movePlugin.setF(F);
    		movePlugin.setV0(V0);
    		//启动
    		this.moveEngine.keep();
    	}
    	
    }
	private void initMoveLinstener(){
	    System.out.println("初始化移动监听器 ，实现基于移动的事件开发");
		movelistener=new MoveAdapter(){
			@Override
			public void runBefore() {
				OutOffByRunBefor();
			}
			@Override
			public void runAfter() {
				// 设置运算后的控件的位置
				Point p = movePlugin.getPoint();
				comp.setLocation(p.x, p.y);

				// 在运算结束时候调用的方法
				OutOffByRunAfter();
			}
			@Override
			public void whenPause() {
				pauseStop();
			}
		};
		
		 
	}
	protected void pauseStop(){
		
	}
	
	protected abstract void OutOffByRunBefor();
	
	
	// 在运算结束时候调用的方法
	protected abstract void OutOffByRunAfter();
	
	/**
	 * 检查是否移动到指定地点
	 * @return
	 */
	public boolean ismoveEnd(){
		return movePlugin.getMoveLength()>=length;
	}
	
	public void distoray(){
		this.moveEngine.distoraySelf();
	}
}
